package com.ayf.payment.game.api.controller.platform;

import com.ayf.payment.game.api.dto.MerchantLevelRelationDTO;
import com.ayf.payment.game.api.dto.OrderInfoDTO;
import com.ayf.payment.game.api.dto.PageDTO;
import com.ayf.payment.game.api.dto.criteria.MerchantCriteria;
import com.ayf.payment.game.api.dto.criteria.MerchantLevelRelationCriteria;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantLevelRelation;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.MerchantLevelRelationService;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wwl
 * @description:
 * @date 2020/9/15 15:04
 * 公众号关联列接口类
 */
@RequestMapping("/t/mlr")
@RestController
public class MerchantRelationController {

    @Autowired
    private MerchantLevelRelationService merchantLevelRelationService;


    @Autowired
    private MerchantService merchantService;

    /**
     * 分页查询关联商列表
     * @param criteria
     * @return
     */
    @PostMapping("/selectPage")
    public CommonResult<PageDTO> selectPage(@RequestBody MerchantLevelRelationCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<MerchantLevelRelationDTO> listPage=merchantLevelRelationService.selectPageByCriteria(criteria);
        PageInfo<MerchantLevelRelationDTO> pageInfo = new PageInfo<>(listPage);
        PageDTO pageDto = BeanConvertUtils.convert(pageInfo, PageDTO.class);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", pageDto);
    }

    /**
     * 根据一级商户ID获取下级商户列表
     * @param dto
     * @return
     */
    @PostMapping("/selectBySupMerchantId")
    public CommonResult<List<MerchantLevelRelationDTO>> selectBySupMerchantId(@RequestBody MerchantLevelRelationDTO dto){
        List<MerchantLevelRelationDTO> list=merchantLevelRelationService.selectDTOBySupMerchantId(dto.getSuperMerchantId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS,list);
    }

    /**
     * 根据商户ID获取商户信息
     * @param dto
     * @return
     */
    @PostMapping("/getMerchantInfo")
    public CommonResult<MerchantLevelRelationDTO> getMerchant(@RequestBody MerchantLevelRelationDTO dto){
        MerchantLevelRelationDTO merchantLevelRelationDTO=merchantLevelRelationService.getDTOByMerchantId(dto.getMerchantId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS,merchantLevelRelationDTO);
    }

    /**
     * 绑定商户
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public CommonResult saveMerchantLevelRelation(@RequestBody MerchantLevelRelationDTO dto){
        if (dto == null) {
            throw new ParameterException("参数不能为空");
        }
        if (dto.getMerchantId() == null) {
            throw new ParameterException("商户ID不能为空");
        }
        if (dto.getMerchantLevel() == null) {
            throw new ParameterException("商户等级不能为空");
        }

        if(dto.getMerchantLevel()<1 && dto.getMerchantLevel()>2){
            throw new ParameterException("选择商户等级错误");
        }
        if(dto.getMerchantLevel()==1){
            dto.setSuperMerchantId(0);
        }
        if(dto.getMerchantLevel()==2){
            if (dto.getSuperMerchantId() == null) {
                throw new ParameterException("上级商户ID不能为空");
            }
            if(dto.getMerchantId().intValue()==dto.getSuperMerchantId().intValue()){
                throw new ParameterException("同一商户不能绑定！");
            }
            List<MerchantLevelRelationDTO> list=merchantLevelRelationService.selectDTOBySupMerchantId(dto.getMerchantId());
            if(null!=list && list.size()>0){
                throw new ParameterException("当前公众号有下挂的二级商户，请解除关联后再进行设置！");
            }
            Merchant merchant= merchantService.find(dto.getSuperMerchantId());
            if(null==merchant){
                throw new ParameterException("上级商户ID不存在！");
            }else{
               if(merchant.getMerchantLevel()==2){
                   throw new ParameterException("此商户为二级商户！");
               }
            }
        }
        MerchantLevelRelation MerchantLevelRelation=new MerchantLevelRelation();
        MerchantLevelRelation.setMerchantId(dto.getMerchantId());
        MerchantLevelRelation.setSuperMerchantId(dto.getSuperMerchantId());
        merchantLevelRelationService.insert(MerchantLevelRelation,dto.getMerchantLevel());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "保存成功","");
    }

}
